﻿using Microsoft.Extensions.Caching.Memory;
using OA.Core.Helper;
using OA.Core.Model;
using OA.Model;
using OA.Model.Api;
using OA.Model.Result;
using OA.Model.View;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OA.BLL
{
    public class CDepartment : CBase
    {
        public static Department GetDepartmentByID(int id,List<Department> list)
        {
            Department info = list.Where(m => m.ID == id).FirstOrDefault();
            if (info == null) { info = new Department() { Name = "" }; }
            return info;
        }

        private static void GetFullDepartmentNameByID(int index,int id, List<Department> list,ref List<string> departmentName)
        {
            if (id <= 0) return;
            if (index > 10) return;
            Department info = list.Where(m => m.ID == id).FirstOrDefault();
            if (info == null) { return ; }
            else
            {
                departmentName.Add(info.Name);
                GetFullDepartmentNameByID(index++,info.ParentDepartmentID, list, ref departmentName);
            }            
        }

        private static void GetFullDepartmentNameByID(int index, int id, List<SimpleDepartment> list, ref List<string> departmentName)
        {
            if (id <= 0) return;
            if (index > 10) return;
            SimpleDepartment info = list.Where(m => m.ID == id).FirstOrDefault();
            if (info == null) { return; }
            else
            {
                departmentName.Add(info.Name);
                GetFullDepartmentNameByID(index++, info.ParentDepartmentID, list, ref departmentName);
            }
        }

        public static string GetFullDepartmentNameByID(int id, List<Department> list)
        {
            List<string> departmentName = new List<string>();
            GetFullDepartmentNameByID(1,id, list, ref departmentName);

            if (departmentName.Count == 0) return string.Empty;
            if (departmentName.Count == 1) return departmentName[0];
            departmentName.RemoveAt(departmentName.Count - 1);
            departmentName.Reverse();
            if (departmentName.Count > 2)
            {
                departmentName.RemoveAt(0);
            }
            return string.Join(" ", departmentName);
        }

        public static string GetFullDepartmentNameByID(int id, List<SimpleDepartment> list)
        {
            List<string> departmentName = new List<string>();
            GetFullDepartmentNameByID(1, id, list, ref departmentName);

            if (departmentName.Count == 0) return string.Empty;
            if (departmentName.Count == 1) return departmentName[0];
            departmentName.RemoveAt(departmentName.Count - 1);
            departmentName.Reverse();
            if (departmentName.Count >= 3)
            {
                departmentName.RemoveAt(0);
            }
            return string.Join(" ", departmentName);
        }



        public static List<int> GetFullDepartmentIDByID(int id, List<SimpleDepartment> list)
        {
            List<int> departments = new List<int>();            
            SimpleDepartment simpleDepartment = list.Where(m => m.ID == id).FirstOrDefault();
            while(simpleDepartment!=null)
            {
                departments.Add(id);
                simpleDepartment = list.Where(m => m.ID == simpleDepartment.ParentDepartmentID).FirstOrDefault();
            }
            departments.Reverse();
            return departments;
        }


        public static List<DepartmentInfoResult> GetDepartments(IMemoryCache _cache, List<DepartmentInfo> departmentList)
        {
            DepartmentInfoResult result = new DepartmentInfoResult();
            CacheHelper cacheHelper = new CacheHelper(_cache);

            List<Department> listDepartment = cacheHelper.GetDepartments();
            List<int> departmentIDs = cacheHelper.GetVEmployeePositions().Select(m => m.DepartmentID).ToList();

            List<Department> topDepartments = new List<Department>();

            topDepartments = listDepartment.Where(m => m.ParentDepartmentID == 0).ToList();
            if (topDepartments.Count == 0) return new List<DepartmentInfoResult>();
            if (topDepartments.Count == 1 && departmentIDs.Contains(topDepartments[0].ID))//顶级节点只有一个，并且下面没有人
            {
                topDepartments = listDepartment.Where(m => m.ParentDepartmentID == topDepartments[0].ID).ToList();
            }

            if (topDepartments.Count == 0) return new List<DepartmentInfoResult>();

            List<DepartmentInfoResult> list = new List<DepartmentInfoResult>();

            foreach (var item in topDepartments)
            {
                DepartmentInfoResult info = new DepartmentInfoResult();
                info.id = item.ID;
                info.name = item.Name;
                info.Path = new List<int>() { item.ID };
                GetChildren(info, listDepartment, departmentList);
                list.Add(info);
                departmentList.Add(new DepartmentInfo() { ID= item.ID,Name=item.Name,Path=info.Path, Depth =info.Path.Count});
            }
            return list;

        }

        public static List<KeyValue> GetDepartmentPath(IMemoryCache _cache,int departmentID)
        {
            CacheHelper cacheHelper = new CacheHelper(_cache);
            List<Department> listDepartment = cacheHelper.GetDepartments();
            List<KeyValue> paths = new List<KeyValue>();
            var info = listDepartment.Where(m => m.ID == departmentID).Select(m => new KeyValue2() { Value = m.ID, Text = m.Name, ParentID = m.ParentDepartmentID }).FirstOrDefault();
            if (info == null) return new List<KeyValue>();
            paths.Add(info);
           
            int tempID = info.ParentID;
            int index = 0;
            while(index<20)
            {
                index++;
                var parent = listDepartment.Where(m => m.ID == tempID).Select(m=> new KeyValue2() { Value = m.ID, Text = m.Name, ParentID = m.ParentDepartmentID }).FirstOrDefault();
                if(parent!=null)
                {
                    paths.Add(parent);
                    tempID = parent.ParentID;
                }
                else
                {
                    break;
                }
            }
            paths.Reverse();
            return paths;

        }


        public static void GetChildren(DepartmentInfoResult info, List<Department> departments, List<DepartmentInfo> departmentList)
        {
            var children = departments.Where(m => m.ParentDepartmentID == info.id).Select(m => new DepartmentInfoResult() { id = m.ID, name = m.Name }).ToList();
            if(children.Count>0)
            {
                 
                info.child = new List<DepartmentInfoResult>();
                info.child.Add(new DepartmentInfoResult() { id=0,name="请选择"});
                info.child.AddRange(children);                 
                foreach (var item in info.child)
                {
                    item.Path = JsonHelper.DeserializeList<int>(JsonHelper.SerializeObject(info.Path));
                    item.Path.Add(item.id);
                    departmentList.Add(new DepartmentInfo() { ID = item.id, Name = item.name, Path = item.Path, Depth = item.Path.Count });
                    if (item.id == 0) continue;
                    GetChildren(item, departments, departmentList);
                }
            }

        }


    }
}
